package com.itheima.thread.utils.exchanger.demo02;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Exchanger;

public class ExchangerUseDemo {

    public static void main(String[] args) {

        Exchanger<ArrayList<String>> exchanger = new Exchanger<>();

        new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader bufferedReader = null;
                try {
                    ArrayList<String> arrayList = new ArrayList<>();
                    bufferedReader = new BufferedReader(new FileReader("itheima-thread-utils\\src\\com\\itheima\\thread\\utils\\exchanger\\demo02\\1.txt"));
                    String line = null;
                    while ((line = bufferedReader.readLine()) != null){
                        arrayList.add(line);
                    }
                    ArrayList<String> exchange = exchanger.exchange(arrayList);
                    if (arrayList.size() == exchange.size()){
                        for (int i = 0; i < arrayList.size(); i++) {
                            if (!arrayList.get(i).equals(exchange.get(i))){
                                System.out.println("数据存在问题");
                            }
                        }
                    } else {
                        System.out.println("数据存在问题");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader bufferedReader = null;
                try {
                    ArrayList<String> arrayList = new ArrayList<>();
                    bufferedReader = new BufferedReader(new FileReader("itheima-thread-utils\\src\\com\\itheima\\thread\\utils\\exchanger\\demo02\\2.txt"));
                    String line = null;
                    while ((line = bufferedReader.readLine()) != null){
                        arrayList.add(line);
                    }
                    ArrayList<String> exchange = exchanger.exchange(arrayList);
                    if (arrayList.size() == exchange.size()){
                        for (int i = 0; i < arrayList.size(); i++) {
                            if (!arrayList.get(i).equals(exchange.get(i))){
                                System.out.println("数据存在问题");
                            }
                        }
                    } else {
                        System.out.println("数据存在问题");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();

    }

}
